package com.example.demo.controller;

import com.example.annotation.MethodCount;
import com.example.aspect.MethodCountAspect;
import com.example.exception.BusinessException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;

@Controller
@RequestMapping("/api")
public class UserController {

    @Autowired
    private MethodCountAspect methodCountAspect;

    @GetMapping(value = "/test")
    @ResponseBody
    public String test() {
        return "1";
    }

    @GetMapping(value = "/methodCountTest")
    @ResponseBody
    @MethodCount
    public String methodCountTest() {
        int methodCountTest = methodCountAspect.getMethodCount("methodCountTest");
        return "方法被访问了："+methodCountTest+" 次";
    }

    @GetMapping(value = "/{id}")
    @ResponseBody
    public ResponseEntity<Object> getUser(@PathVariable String id) throws Exception {
        //throw new BusinessException("测试全局异常", HttpStatus.BAD_REQUEST);
        if ("1".equals(id)){
            throw new Exception();
        }
        if ("2".equals(id)){
            throw new IOException();
        }
        if ("3".equals(id)){
            throw new NullPointerException();
        }
        throw new BusinessException("自定义异常",HttpStatus.BAD_REQUEST);
    }



}
